<?php
namespace Framework\Http;


class Response
{
    const FORMAT_RAW = 'raw';
    const FORMAT_HTML = 'html';
    const FORMAT_JSON = 'json';
    const FORMAT_JSONP = 'jsonp';
    const FORMAT_XML = 'xml';

    public $format = self::FORMAT_HTML;

    //默认响应的HTTP状状态码
    private $_statusCode = 200;

    private $_headers;

    public $data;


    public function getStatusCode()
    {
        return $this->_statusCode;
    }

    //->header('Content-Type', 'text/plain');
    public function header($key, $values)
    {
    	$this->_headers[$key] = $values;
    	return $this;
    }

    public function getHeaders()
    {
        if ($this->_headers === null) {
            $this->_headers = [];
        }

        return $this->_headers;
    }

    /**
     * ->withHeaders([
                'Content-Type' => $type,
                'X-Header-One' => 'Header Value',
                'X-Header-Two' => 'Header Value',
            ])
     */
    public function withHeaders($value='')
    {
    	# code...
    }

    /**
     * 设置cookie
     * @param $name     Cookie 名称
     * @param $value    Cookie 值
     * @param $expires  Cookie 的过期时间
     * @param $path     Cookie 有效的服务器路径
     * @param $domain   Cookie 的有效域名/子域名
     * @param $secure   设置成 true 时，只有安全连接存在时才会设置 Cookie
     * @param $httponly 设置成 true，Cookie 仅可通过 HTTP 协议访问
     */
    public function cookie($name, $value, int $expires=2592000, $path='/', $domain='', $secure=false, $httponly=false)
    {
        setcookie($name, $value, time()+$expires, $path, $domain, $secure, $httponly);

        return $this;
    }

    //重定向

    //重定向并使用闪存的 Session 数据#


    public function json($data = [], $status = 200, array $headers = [], $options = 0)
    {
    	$this->data = json_encode($data);

    	return $this->header('Content-Type', 'application/json');
    }

    public function xml($data = [], $status = 200, array $headers = [], $options = 0)
    {
        
        $this->header('Content-Type', 'text/xml');

        $xml = new \Xml();
        $this->data = $xml-> generateXmlFromArray($data, 'entities', 'entity');


        return $this;
    }

    public function raw($data = [], $status = 200)
    {
        $this->data = json_encode($data);

        return $this->header('Content-Type', 'plain/text');
    }

    /**
     * 重定向
     * @param $url 重定向地址
     */
    public function redirect($url)
    {
        return $this->header('Location', $url);
    }
    
    /**
     * 刷新当前页面
     */
    public function refresh()
    {

    }

	public function send()
	{
		//发送头部
		$this->sendHeaders();

		//发送内容
		$this->sendContent();
	}

    public function sendHeaders()
    {
        // 已经发送过
        if (headers_sent()) {
            return $this;
        }

        // headers
        if($this->_headers){
        	foreach ($this->getHeaders() as $name => $value) {
        		$name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
        		header("$name: $value");
        	}
        }

        //cookie

        //状态码
        http_response_code($this->_statusCode);
    }

    public function sendContent()
    {
        echo $this->data;
    }
}